import { RuleInfoConfigList, addList, editList } from "@/api/yybl/jinkurule";

export default {
  name: "config",
  data() {
    return {
      configVisible: false,
      ruleId: "",
      ruleName: "",
      ruleName: "",
      baseForm: {
        ruleName: "",
        demoList: [
          {
            matchType: "",
            matchKey: "",
            matchValue: "",
          },
        ],
      },
      configList: [
        {
          value: "exact",
          label: "完全匹配",
        },
        {
          value: "regex",
          label: "正则匹配",
        },
      ],
      configKeysList: [
        {
          value: "method",
          label: "method",
        },
        {
          value: "path",
          label: "path",
        },
        {
          value: "time",
          label: "time",
        },
        {
          value: "user",
          label: "user",
        },
        {
          value: "scheme",
          label: "scheme",
        },
        {
          value: "host",
          label: "host",
        },
        {
          value: "uri",
          label: "uri",
        },
        {
          value: "query",
          label: "query",
        },
      ],
      index: 0,
      rules: {
        ruleName: [
          { required: true, message: "请输入规则名称", trigger: "blur" },
        ],
        matchType: [
          {
            required: true,
            message: "",
            trigger: "change",
          },
        ],
        matchKey: [{ required: true, message: "", trigger: "blur" }],
        matchValue: [{ required: true, message: "", trigger: "blur" }],
      },
      catchList: [],
    };
  },
  computed: {},
  created() {},
  methods: {
    handleConfig(val) {
      this.configVisible = true;
      this.getConfigList(val);
    },
    getConfigList(val) {
      const { ruleId, ruleName } = val;
      if (ruleId) {
        this.ruleName = ruleName;
        RuleInfoConfigList({
          ruleId,
        }).then((res) => {
          if (res.rescode == 200) {
            console.log(res.data);
            this.baseForm.demoList = res.data;
            this.baseForm.ruleName = ruleName;
            // this.catchList = edgeParamsConfigsList.map((i) => ({ ...i }));
          } else {
            this.$message.error(res.msg);
          }
        });
      } else {
        this.$message.error("规则ID不存在！");
      }
    },
    addDomain() {
      //先隐藏delFlag为1的数据， 最后过滤掉数组中id为空且delFlag为'1'的数据
      //不隐藏直接删除，最后添加数据库数组中已经删掉的配置项，把delFlag修改为1
      this.baseForm.demoList.push({
        matchType: "",
        matchKey: "",
        matchValue: "",
      });
    },
    deleteDomain(item) {
      this.$confirm("此操作将删除参数配置项, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(() => {
        var index = this.baseForm.demoList.indexOf(item);
        if (index !== -1) {
          this.baseForm.demoList.splice(index, 1);
        }
      });
    },
    handleConfigClose() {
      this.configVisible = false;
    },
    submitConfigForm() {
      console.log(this.$refs.baseForm);
      this.$refs.baseForm.validate((valid) => {
        if (valid) {
          // this.$confirm("您确定【提交】？", "提示", {
          //   confirmButtonText: "确定",
          //   cancelButtonText: "取消",
          //   type: "warning",
          // }).then(() => {

          // });
          const { demoList } = this.baseForm;
          // console.log("校验通过", demoList, this.catchList);
          // // 利用缓存增加数组参数数据库有的只允许物理删除
          // const arr = this.catchList.map((item) => {
          //   const index = demoList.findIndex((i) => i.id == item.id);
          //   return index == -1 ? { ...item, delFlag: "2" } : "";
          // });
          let params = {
            ontrolRuleInfo: [...demoList],
            ruleName: this.baseForm.ruleName,
          };

          // params.userId = null;
          // params.sharedkey = encode(form.sharedkey);
          if (this.editFlag) {
            params.ruleId = this.curRow.ruleId;
            editList(params).then((res) => {
              if (res.rescode == 200) {
                this.$message.success(res.msg);
                this.configVisible = false;
                this.refresh();
              } else {
                this.$message.error(res.msg);
              }
            });
          } else {
            addList(params).then((res) => {
              if (res.rescode == 200) {
                this.$message.success(res.msg);
                this.configVisible = false;
                this.refresh();
              } else {
                this.$message.error(res.msg);
              }
            });
          }
        }
      });
    },
  },
};
